﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace demo.Common
{
    public class Utils
    {
        /// <summary>
        /// 获得当前绝对路径
        /// </summary>
        /// <param name="strPath">指定的路径</param>
        /// <returns>绝对路径</returns>
        public static string GetMapPath(string strPath)
        {
            if (strPath.ToLower().StartsWith("http://"))
            {
                return strPath;
            }
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Server.MapPath(strPath);
            }
            else //非web程序引用
            {
                strPath = strPath.Replace("/", "\\");
                if (strPath.StartsWith("\\"))
                {
                    strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
                }
                return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
            }
        }

        /// <summary>
        /// 取得客户端真实IP。如果有代理则取第一个非内网地址
        /// </summary>
        public static string IPAddress
        {
            get
            {
                var result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (!string.IsNullOrEmpty(result))
                {
                    //可能有代理
                    if (result.IndexOf(".", StringComparison.Ordinal) == -1) result = null;
                    else
                    {
                        //有“,”，估计多个代理。取第一个不是内网的IP。
                        result = result.Replace(" ", "").Replace("'", "");
                        string[] tempAryIp = result.Split(',');
                        foreach (string t in tempAryIp)
                        {
                            if (t.Substring(0, 3) != "10." &&
                                t.Substring(0, 7) != "192.168" &&
                                t.Substring(0, 7) != "172.16." &&
                                IsIpAddress(t))
                            {
                                return t;    //找到不是内网的地址
                            }
                        }

                        if (IsIpAddress(result)) return result; //代理即是IP格式 ,IsIPAddress判断是否是IP的方法, 
                        result = null;                          //代理中的内容 非IP，取IP
                    }

                }

                //string IpAddress = (
                //    HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null &&
                //    HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != string.Empty) ? 
                //    HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : 
                //    HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

                if (string.IsNullOrEmpty(result))
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

                if (string.IsNullOrEmpty(result))
                    result = HttpContext.Current.Request.UserHostAddress;

                return result;
            }
        }

        private static bool IsIpAddress(string ipAddress)
        {
            IPAddress ip = null;
            return System.Net.IPAddress.TryParse(ipAddress, out ip);
        }
    }
}
